﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;

namespace KD.Sms.DAL.SqlLite
{
    internal sealed partial class Main
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="chinesename"></param>
        /// <returns></returns>
        public DataTable DoQueryUserList(string chinesename)
        {
            _strSql = "select User_Id,User_Acct,Chinese_name,case when User_State = 0 then '禁用' else '启用' end as User_State,case when User_KeyInformation = 0 then '隐藏' else '显示' end as User_KeyInformation,'修改' as XG,User_Pwd,case when User_State = 0 then '启用' else '禁用' end as CZ,case when User_KeyInformation = 0 then '显示' else '隐藏' end as XS from sysUser where User_Acct !='admin' ";
            if (!string.IsNullOrEmpty(chinesename))
                _strSql += string.Format(" and Chinese_name like '%{0}%' or User_Acct like '%{0}%'", chinesename);
            _strSql += " order by Chinese_name ";
            return SQLiteHelper.GetDataTable(_strSql);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="chinese"></param>
        /// <param name="acct"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool DoAddUser(string chinese, string acct, string pwd)
        {
            _strSql = string.Format("insert into SysUser(User_id,User_Acct,User_Pwd,Chinese_Name,User_State,User_KeyInformation)values(IFNULL((SELECT MAX(User_id) FROM SysUser), 0) + 1,'{0}','{1}','{2}',0,0)", acct, pwd, chinese);
            return SQLiteHelper.ExecuteNonQuery(_strSql, null) > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="chinese"></param>
        /// <param name="acct"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool DoUpdateUser(string userid, string chinese, string acct, string pwd)
        {
            _strSql = string.Format("update sysUser set User_Acct= '{0}',Chinese_name='{1}',User_Pwd='{2}',User_State = 1,User_KeyInformation = 0 where User_id = {3}", acct, chinese, pwd, userid);
            return SQLiteHelper.ExecuteNonQuery(_strSql, null) > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool DoUpdateUserState(string userid, bool value)
        {
            _strSql = string.Format("update sysUser set User_State = {0} where User_id = {1}", Convert.ToInt32(value), userid);
            return SQLiteHelper.ExecuteNonQuery(_strSql, null) > 0;
        }

        /// <summary>
        /// 更新用户显示或隐藏
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool DoUpdateUserKeyInformation(string userid, bool value)
        {
            _strSql = string.Format("update sysUser set User_KeyInformation = {0} where User_id = {1}", Convert.ToInt32(value), userid);
            return SQLiteHelper.ExecuteNonQuery(_strSql, null) > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool DoChangeUserPwd(int userid, string pwd)
        {
            _strSql = string.Format("update sysUser set User_Pwd = '{0}' where User_id = {1}", pwd, userid);
            return SQLiteHelper.ExecuteNonQuery(_strSql, null) > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable DoQueryAdminState()
        {
            return SQLiteHelper.GetDataTable(string.Format("select User_State from sysUser where User_Acct = 'admin'"));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool DoUpdateAdminState(bool _bState)
        {
            _strSql = string.Format("update sysUser set User_State = {0} where User_Acct = 'admin'", Convert.ToInt32(_bState));
            return SQLiteHelper.ExecuteNonQuery(_strSql, null) > 0;
        }
    }
}
